<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/3/2
 * Time: 上午9:25
 */

class Pass extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function login($teachtype = "")
    {
        if (empty($teachtype)) die('request forbidden');
        $scope='snsapi_base';
        $redirect_uri = site_url('/teach/pass/logincallback/'.$teachtype);
        $this->load->library('openlogin/wechat');
        $this->wechat->set_scope($scope);
        $this->wechat->set_redirect_uri($redirect_uri);
        //跳转到引导页，静默授权
        $this->wechat->get_code();
    }

    public function logincallback($teachtype = "")
    {
        if (empty($teachtype)) die('request forbidden');
        //获取openid
        $this->load->library('openlogin/wechat');
        $this->wechat->get_access_token();
        $openid=$this->wechat->get_openid();
        //检查用户是否已经注册
        $this->load->model('user_model');
        $uid=$this->user_model->getUidByWxopenid($openid);
        //用户已经注册
        if($uid !== false){
            //设置登陆session
            if(!isset($_SESSION)) session_start();
            $_SESSION['uid'] = $uid;
            $_SESSION['wx_openid'] = $openid;
            //判断学员参加的是哪次课程
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            $stutype = $redis->get('teach:'.$teachtype.':stu:'.$uid);
            //是否已经录入学员课程期数
            if ($stutype !==  false){
                $url = $this->getRedUrl($stutype);
                if ($url!==false){
                    redirect($url);
                }else{
                    $this->load->view('teach/choose_'.$teachtype);
                }
            }else{$this->load->view('teach/choose_'.$teachtype);};
        }
        //没有注册，获取用户基本信息
        else{
            $this->firstlogin($teachtype);
        }
    }

    public function commonlogin($teachtype)
    {
        $this->load->view('teach/choose_'.$teachtype);
    }

    //第一次登陆
    public function firstlogin($teachtype = "")
    {
        if (empty($teachtype)) die('request forbidden');
        $redirect_uri=site_url('/teach/pass/firstlogincallback/'.$teachtype);
        $scope='snsapi_userinfo';
        $this->wechat->set_scope($scope);
        $this->wechat->set_redirect_uri($redirect_uri);
        //跳转到引导页，授权登陆
        $this->wechat->get_code();
    }

    public function firstlogincallback($teachtype = "")
    {
        if (empty($teachtype)) die('request forbidden');
        $this->load->library('openlogin/wechat');
        $this->wechat->get_access_token();
        $info=$this->wechat->get_user_info();
        //检查用户是否已经注册
        $this->load->model('user_model');
        $res=$this->user_model->getUser(array('wx_unionid'=>$info['unionid']),'uid');

        if($info!==false){
            if (empty($res)){
                //新用户注册
                $data = array(
                    'utype'      => 'weixin',
                    'wx_openid'  => $info['openid'],
                    'wx_unionid' => $info['unionid'],
                    'userName'   => $info['nickname'],
                    'userToken'  => md5($info['access_token']),
                    'userIcon'   => $info['userIcon'],
                    'status'     => 1,
                    'created'    => time(),
                    'changed'    => time()
                );
                $uid = $this->user_model->createUser($data);
                if($uid){
                    $this->user_model->createUserInfo(array('uid' => $uid, 'userGender'=>$info['gender']));
                    //设置登陆session
                    if(!isset($_SESSION)) session_start();
                    $_SESSION['uid'] = $uid;
                    $_SESSION['wx_openid'] = $info['openid'];
                    //设置登陆信息
                    $this->load->model('redis_model');
                    $redis = $this->redis_model->getRedisInstance();
                    $redis->multi();
                    $redis->set('user:'.$info['openid'],$uid);
                    //新用户赠送100能量豆
                    $redis->incrby('user:'.$uid.'.beans',100);
                    $redis->exec();
                    //判断学员参加的是哪次课程
                    $stutype = $redis->get('teach:'.$teachtype.':stu:'.$uid);
                    //是否已经录入学员课程期数
                    if ($stutype !==  false){
                        $url = $this->getRedUrl($stutype);
                        if ($url!==false){
                            redirect($url);
                        }else{
                            $this->load->view('teach/choose_'.$teachtype);
                        }
                    }else{$this->load->view('teach/choose_'.$teachtype);};
                }else{die("系统错误，请重试");}
            }else{
                //用户存在，同步redis数据
                $this->user_model->setUidWithOpenid($info['openid'],$res['uid']);
            }
        }else{die("系统错误，请重试");}
    }

    public function getRedUrl($stutype)
    {
        switch ($stutype){
            case "adv1":
                $url = "https://st.h5.xiaoe-tech.com/st/9A5nnuYtf";
                break;
            case "adv2":
                $url = "https://st.h5.xiaoe-tech.com/st/5hsxj0r5b";
                break;
            case "qf1":
                $url = "";
                break;
            default:
                $url = false;
        }
        return $url;
    }

    public function chooseadv($stutype = "")
    {
        if (empty($stutype)) die("request forbidden");
        if (!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $url = $this->getRedUrl($stutype);
            if ($url!==false){
                //录入学员类型
                $this->load->model('redis_model');
                $redis = $this->redis_model->getRedisInstance();
                $redis->set('teach:adv:stu:'.$_SESSION['uid'],$stutype);
                redirect($url);
            }else{die("request forbidden");}
        }else{redirect("http://api.nldou.com/teach/pass/login/adv");}
    }

    public function chooseqf($stutype = "")
    {
        if (empty($stutype)) die("request forbidden");
        if (!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $url = $this->getRedUrl($stutype);
            if ($url!==false){
                //录入学员类型
                $this->load->model('redis_model');
                $redis = $this->redis_model->getRedisInstance();
                $redis->set('teach:adv:qf:stu:'.$_SESSION['uid'],$stutype);
                redirect($url);
            }else{die("request forbidden");}
        }else{redirect("http://api.nldou.com/teach/pass/login/qf");}
    }


}